﻿// <copyright file="CreateAliasExtensions.cs" company="Clever Software">
// Copyright (c) 2013, 2014 All Right Reserved, http://www.cleversoftware.by
//
// This source is subject to the Microsoft Permissive License.
// Please see the License.txt file for more information.
// All other rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// </copyright>
// <author>Clever Software</author>
// <email>info@cleversoftware.by</email>
// <date>2014-02-04</date> 
// <summary>CleverCMS source code</summary>

namespace CleverCMS.Core.Extensions
{
    using System;
    using System.Text.RegularExpressions;
    
    public static class CreateAliasExtensions
    {
        public static string CreateAlias(string title, int? id)
        {
            // convert multiple spaces into one space
            String rezult = Regex.Replace(title.ToLower(), @"\s+", " ").Trim();   
            rezult = GetLatinCodeFromCyrillic(rezult);
            
            // invalid chars
            rezult = Regex.Replace(rezult, @"[^a-z0-9\s-]", "");

            // cut and trim it 
            rezult = rezult.Substring(0, rezult.Length <= 80 ? rezult.Length : rezult.LastIndexOf(' ', 80)).Trim();

            // hyphens
            rezult = Regex.Replace(rezult, @"\s", "-");    

            return String.Format((id.HasValue ? "{0}-{1}" : "{0}"), rezult, id);
        }

        static string GetLatinCodeFromCyrillic(string str)
        {
            str = str.Replace("б", "b");
            str = str.Replace("в", "v");
            str = str.Replace("г", "g");
            str = str.Replace("х", "h");
            str = str.Replace("д", "d");
            str = str.Replace("э", "ye");
            str = str.Replace("ж", "zh");
            str = str.Replace("з", "z");
            str = str.Replace("и", "i");
            str = str.Replace("ы", "i"); ;
            str = str.Replace("й", "y");
            str = str.Replace("к", "k");
            str = str.Replace("л", "l");
            str = str.Replace("м", "m");
            str = str.Replace("н", "n");
            str = str.Replace("п", "p");
            str = str.Replace("р", "r");
            str = str.Replace("с", "s");
            str = str.Replace("ч", "ch");
            str = str.Replace("ш", "sh");
            str = str.Replace("щ", "shch");
            str = str.Replace('ъ', '"');
            str = str.Replace("ю", "yu");
            str = str.Replace("я", "ya");
            str = str.Replace('ь', '"');
            str = str.Replace('т', 't');
            str = str.Replace('ц', 'c');
            str = str.Replace('о', 'o');
            str = str.Replace('е', 'e');
            str = str.Replace('ё', 'e');
            str = str.Replace('а', 'a');
            str = str.Replace('ф', 'f');
            str = str.Replace("у", "u");
            str = str.Replace('х', 'x');
            return str;
        }
    }
}